package com.kma.summer2012.dao;

import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.model.CrumbsClient;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 15.01.13
 * Time: 17:32
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsClientDAOImpl implements CrumbsClientDAO {

    @Override
    public void add(CrumbsClient client) {

        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(client);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {


                session.close();

            }

        }

    }

    @Override
    public CrumbsClient addClient(CrumbsClient client) throws SQLException {

        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            CrumbsClient client1 = (CrumbsClient) session.createCriteria(CrumbsClient.class).add(Expression.eq("adress", client.getAdress())).add(Expression.eq("mail", client.getMail())).add(Expression.eq("name", client.getName())).uniqueResult();
            if (client1 == null) {
                session.save(client);
            } else {
                client = client1;
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {

                session.close();

            }
            return client;
        }

    }

    @Override
    public void update(CrumbsClient client) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(client);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsClient getById(int client_id) {
        Session session = null;
        CrumbsClient client = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            client = (CrumbsClient) session.get(CrumbsClient.class, client_id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return client;
    }

    @Override
    public Collection<CrumbsClient> getAll() {
        Session session = null;
        List clients = new ArrayList<CrumbsClient>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            clients = session.createCriteria(CrumbsClient.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return clients;
    }

    @Override
    public void delete(CrumbsClient client) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(client);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }


}
